package com.wrench.elevator.tida.controller;

import com.alibaba.fastjson.JSON;
import com.wrench.elevator.tida.constant.Result;
import com.wrench.elevator.tida.domain.Page;
import com.wrench.elevator.tida.exception.WrenchException;
import com.wrench.elevator.tida.service.BuildingService;
import com.wrench.elevator.tida.util.WrenchUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("/building_manage")
public class BuildingManageController {
    private static final Logger logger = LoggerFactory
            .getLogger(BuildingManageController.class);

    @Resource
    private BuildingService buildingService;

    @RequestMapping("queryDanyuanByBuildingName")
    public void queryDanyuanByBuildingName(HttpServletRequest request,
                                         HttpServletResponse response, ModelMap map){
        logger.debug("==>**********enter in queryDanyuanByProvinceId(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
        String wuye_id=WrenchUtil.getPara(request,"wuye_id");
        String building_name=WrenchUtil.getPara(request,"building_name");
        String current_page=WrenchUtil.getPara(request,"current_page");
        String limit=WrenchUtil.getPara(request,"limit");
        Page page=null;
        WrenchException error=null;
        try {
            page=buildingService.queryDanyuanByBuildingName(building_name,wuye_id,current_page,limit);
        } catch (WrenchException e) {
            error=e;
        }
        if (WrenchUtil.hasNull(error)) {
            map.clear();
            map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
            map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
            map.put("message", "queryDanyuanByBuildingName success");
            map.put("page",page);

        } else {
            map.clear();
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE, error.getResultMessage());
        }
        logger.debug("==> response = {}", JSON.toJSON(map));
        WrenchUtil.responseMap(response, map);
        map = null;
        logger.debug("==>**********exit out queryDanyuanByBuildingName(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");

    }

    @RequestMapping("queryDanyuanByProvinceId")
    public void queryDanyuanByProvinceId(HttpServletRequest request,
                           HttpServletResponse response, ModelMap map){
        logger.debug("==>**********enter in queryDanyuanByProvinceId(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
       String wuye_id=WrenchUtil.getPara(request,"wuye_id");
       String province_id=WrenchUtil.getPara(request,"province_id");
       String current_page=WrenchUtil.getPara(request,"current_page");
       String limit=WrenchUtil.getPara(request,"limit");
       Page page=null;
       WrenchException error=null;
        try {
            page=buildingService.queryDanyuanByProvinceId(province_id,wuye_id,current_page,limit);
        } catch (WrenchException e) {
            error=e;
        }
        if (WrenchUtil.hasNull(error)) {
            map.clear();
            map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
            map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
            map.put("message", "queryDanyuanByProvinceId success");
            map.put("page",page);

        } else {
            map.clear();
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE, error.getResultMessage());
        }
        logger.debug("==> response = {}", JSON.toJSON(map));
        WrenchUtil.responseMap(response, map);
        map = null;
        logger.debug("==>**********exit out queryDanyuanByProvinceId(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");

    }

    @RequestMapping("queryDanyuanByCityId")
    public void queryDanyuanByCityId(HttpServletRequest request,
                                         HttpServletResponse response, ModelMap map){
        logger.debug("==>**********enter in queryDanyuanByCityId(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
        String wuye_id=WrenchUtil.getPara(request,"wuye_id");
        String city_id=WrenchUtil.getPara(request,"city_id");
        String current_page=WrenchUtil.getPara(request,"current_page");
        String limit=WrenchUtil.getPara(request,"limit");
        Page page=null;
        WrenchException error=null;
        try {
            page=buildingService.queryDanyuanByCityId(city_id,wuye_id,current_page,limit);
        } catch (WrenchException e) {
            error=e;
        }
        if (WrenchUtil.hasNull(error)) {
            map.clear();
            map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
            map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
            map.put("message", "queryDanyuanByCityId success");
            map.put("page",page);

        } else {
            map.clear();
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE, error.getResultMessage());
        }
        logger.debug("==> response = {}", JSON.toJSON(map));
        WrenchUtil.responseMap(response, map);
        map = null;
        logger.debug("==>**********exit out queryDanyuanByCityId(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");

    }

    @RequestMapping("queryDanyuanByDistrictId")
    public void queryDanyuanByDistrictId(HttpServletRequest request,
                                         HttpServletResponse response, ModelMap map){
        logger.debug("==>**********enter in queryDanyuanByDistrictId(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
        String wuye_id=WrenchUtil.getPara(request,"wuye_id");
        String district_id=WrenchUtil.getPara(request,"district_id");
        String current_page=WrenchUtil.getPara(request,"current_page");
        String limit=WrenchUtil.getPara(request,"limit");
        Page page=null;
        WrenchException error=null;
        try {
            page=buildingService.queryDanyuanByDistrictId(district_id,wuye_id,current_page,limit);
        } catch (WrenchException e) {
            error=e;
        }
        if (WrenchUtil.hasNull(error)) {
            map.clear();
            map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
            map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
            map.put("message", "queryDanyuanByDistrictId success");
            map.put("page",page);

        } else {
            map.clear();
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE, error.getResultMessage());
        }
        logger.debug("==> response = {}", JSON.toJSON(map));
        WrenchUtil.responseMap(response, map);
        map = null;
        logger.debug("==>**********exit out queryDanyuanByDistrictId(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");

    }

    @RequestMapping("queryDanyuanByBuildingId")
    public void queryDanyuanByBuildingId(HttpServletRequest request,
                                         HttpServletResponse response, ModelMap map){
        logger.debug("==>**********enter in queryDanyuanByBuildingId(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");

        String building_id=WrenchUtil.getPara(request,"building_id");
        String current_page=WrenchUtil.getPara(request,"current_page");
        String limit=WrenchUtil.getPara(request,"limit");
        Page page=null;
        WrenchException error=null;
        try {
            page=buildingService.queryDanyuanByBuildingId(building_id,current_page,limit);
        } catch (WrenchException e) {
            error=e;
        }
        if (WrenchUtil.hasNull(error)) {
            map.clear();
            map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
            map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
            map.put("message", "queryDanyuanByBuildingId success");
            map.put("page",page);

        } else {
            map.clear();
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE, error.getResultMessage());
        }
        logger.debug("==> response = {}", JSON.toJSON(map));
        WrenchUtil.responseMap(response, map);
        map = null;
        logger.debug("==>**********exit out queryDanyuanByBuildingId(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");

    }

    @RequestMapping("queryDanyuanByDanyuanId")
    public void queryDanyuanByDanyuanId(HttpServletRequest request,
                                         HttpServletResponse response, ModelMap map){
        logger.debug("==>**********enter in queryDanyuanByProvinceId(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");

        String danyuan_id=WrenchUtil.getPara(request,"danyuan_id");
        String current_page=WrenchUtil.getPara(request,"current_page");
        String limit=WrenchUtil.getPara(request,"limit");
        Page page=null;
        WrenchException error=null;
        try {
            page=buildingService.queryDanyuanByDanyuanId(danyuan_id,current_page,limit);
        } catch (WrenchException e) {
            error=e;
        }
        if (WrenchUtil.hasNull(error)) {
            map.clear();
            map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
            map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
            map.put("message", "queryDanyuanByDanyuanId success");
            map.put("page",page);

        } else {
            map.clear();
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE, error.getResultMessage());
        }
        logger.debug("==> response = {}", JSON.toJSON(map));
        WrenchUtil.responseMap(response, map);
        map = null;
        logger.debug("==>**********exit out queryDanyuanByDanyuanId(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");

    }



    @RequestMapping("queryChengtiComplainByProvinceId")
    public void queryChengtiComplainByProvinceId(HttpServletRequest request,
                                                 HttpServletResponse response, ModelMap map){
        logger.debug("==>**********enter in queryChengtiComplainByProvinceId(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
        String wuye_id=WrenchUtil.getPara(request,"wuye_id");
        String province_id=WrenchUtil.getPara(request,"province_id");
        String current_page=WrenchUtil.getPara(request,"current_page");
        String limit=WrenchUtil.getPara(request,"limit");
        Page page=null;
        WrenchException error=null;
        try {
            page=buildingService.queryChengtiComplainByProvinceId(province_id,wuye_id,current_page,limit);
        } catch (WrenchException e) {
            error=e;
        }
        if (WrenchUtil.hasNull(error)) {
            map.clear();
            map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
            map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
            map.put("message", "queryChengtiComplainByProvinceId success");
            map.put("page",page);

        } else {
            map.clear();
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE, error.getResultMessage());
        }
        logger.debug("==> response = {}", JSON.toJSON(map));
        WrenchUtil.responseMap(response, map);
        map = null;
        logger.debug("==>**********exit out queryChengtiComplainByProvinceId(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");

    }



    @RequestMapping("queryChengtiComplainByTel")
    public void queryChengtiComplainByTel(HttpServletRequest request,
                                             HttpServletResponse response, ModelMap map){
        logger.debug("==>**********enter in queryChengtiComplainByCityId(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
        String wuye_id=WrenchUtil.getPara(request,"wuye_id");
        String tel=WrenchUtil.getPara(request,"tel");
        String current_page=WrenchUtil.getPara(request,"current_page");
        String limit=WrenchUtil.getPara(request,"limit");
        Page page=null;
        WrenchException error=null;
        try {
            page=buildingService.queryChengtiComplainByTel(tel,wuye_id,current_page,limit);
        } catch (WrenchException e) {
            error=e;
        }
        if (WrenchUtil.hasNull(error)) {
            map.clear();
            map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
            map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
            map.put("message", "queryChengtiComplainByTel success");
            map.put("page",page);

        } else {
            map.clear();
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE, error.getResultMessage());
        }
        logger.debug("==> response = {}", JSON.toJSON(map));
        WrenchUtil.responseMap(response, map);
        map = null;
        logger.debug("==>**********exit out queryChengtiComplainByTel(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");

    }

    @RequestMapping("queryChengtiComplainByCityId")
    public void queryChengtiComplainByCityId(HttpServletRequest request,
                                             HttpServletResponse response, ModelMap map){
        logger.debug("==>**********enter in queryChengtiComplainByCityId(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
        String wuye_id=WrenchUtil.getPara(request,"wuye_id");
        String city_id=WrenchUtil.getPara(request,"city_id");
        String current_page=WrenchUtil.getPara(request,"current_page");
        String limit=WrenchUtil.getPara(request,"limit");
        Page page=null;
        WrenchException error=null;
        try {
            page=buildingService.queryChengtiComplainByCityId(city_id,wuye_id,current_page,limit);
        } catch (WrenchException e) {
            error=e;
        }
        if (WrenchUtil.hasNull(error)) {
            map.clear();
            map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
            map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
            map.put("message", "queryChengtiComplainByCityId success");
            map.put("page",page);

        } else {
            map.clear();
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE, error.getResultMessage());
        }
        logger.debug("==> response = {}", JSON.toJSON(map));
        WrenchUtil.responseMap(response, map);
        map = null;
        logger.debug("==>**********exit out queryChengtiComplainByCityId(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");

    }

    @RequestMapping("queryChengtiComplainByDistrictId")
    public void queryChengtiComplainByDistrictId(HttpServletRequest request,
                                                 HttpServletResponse response, ModelMap map){
        logger.debug("==>**********enter in queryChengtiComplainByDistrictId(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
        String wuye_id=WrenchUtil.getPara(request,"wuye_id");
        String district_id=WrenchUtil.getPara(request,"district_id");
        String current_page=WrenchUtil.getPara(request,"current_page");
        String limit=WrenchUtil.getPara(request,"limit");
        Page page=null;
        WrenchException error=null;
        try {
            page=buildingService.queryChengtiComplainByDistrictId(district_id,wuye_id,current_page,limit);
        } catch (WrenchException e) {
            error=e;
        }
        if (WrenchUtil.hasNull(error)) {
            map.clear();
            map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
            map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
            map.put("message", "queryChengtiComplainByDistrictId success");
            map.put("page",page);

        } else {
            map.clear();
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE, error.getResultMessage());
        }
        logger.debug("==> response = {}", JSON.toJSON(map));
        WrenchUtil.responseMap(response, map);
        map = null;
        logger.debug("==>**********exit out queryChengtiComplainByDistrictId(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");

    }

    @RequestMapping("queryChengtiComplainByBuildingId")
    public void queryChengtiComplainByBuildingId(HttpServletRequest request,
                                                 HttpServletResponse response, ModelMap map){
        logger.debug("==>**********enter in queryChengtiComplainByBuildingId(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");

        String building_id=WrenchUtil.getPara(request,"building_id");
        String current_page=WrenchUtil.getPara(request,"current_page");
        String limit=WrenchUtil.getPara(request,"limit");
        Page page=null;
        WrenchException error=null;
        try {
            page=buildingService.queryChengtiComplainByBuildingId(building_id,current_page,limit);
        } catch (WrenchException e) {
            error=e;
        }
        if (WrenchUtil.hasNull(error)) {
            map.clear();
            map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
            map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
            map.put("message", "queryChengtiComplainByBuildingId success");
            map.put("page",page);

        } else {
            map.clear();
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE, error.getResultMessage());
        }
        logger.debug("==> response = {}", JSON.toJSON(map));
        WrenchUtil.responseMap(response, map);
        map = null;
        logger.debug("==>**********exit out queryChengtiComplainByBuildingId(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");

    }

    @RequestMapping("queryChengtiComplainByDanyuanId")
    public void queryChengtiComplainByDanyuanId(HttpServletRequest request,
                                                HttpServletResponse response, ModelMap map){
        logger.debug("==>**********enter in queryChengtiComplainByProvinceId(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");

        String danyuan_id=WrenchUtil.getPara(request,"danyuan_id");
        String current_page=WrenchUtil.getPara(request,"current_page");
        String limit=WrenchUtil.getPara(request,"limit");
        Page page=null;
        WrenchException error=null;
        try {
            page=buildingService.queryChengtiComplainByDanyuanId(danyuan_id,current_page,limit);
        } catch (WrenchException e) {
            error=e;
        }
        if (WrenchUtil.hasNull(error)) {
            map.clear();
            map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
            map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
            map.put("message", "queryChengtiComplainByDanyuanId success");
            map.put("page",page);

        } else {
            map.clear();
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE, error.getResultMessage());
        }
        logger.debug("==> response = {}", JSON.toJSON(map));
        WrenchUtil.responseMap(response, map);
        map = null;
        logger.debug("==>**********exit out queryChengtiComplainByDanyuanId(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");

    }


    @RequestMapping(value = "/batchDealComplain")
    public void batchDealComplain(HttpServletRequest request,
                           HttpServletResponse response, ModelMap map){
        logger.debug("==>**********enter in batchDealComplain(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");
        String[] chengti_ids=WrenchUtil.getParas(request,"chengti_id");
        String status=WrenchUtil.getPara(request,"status");
        WrenchException error = null;
        for(String id:chengti_ids) {

            try {
                buildingService.dealComplain(status,id);
            } catch (WrenchException e) {
                error = e;
                break;
            }
        }
        if (WrenchUtil.hasNull(error)) {
            map.clear();
            map.put(Result.RESULT_CODE, Result.SUCCESS_CODE);
            map.put(Result.RESULT_MESSAGE, Result.SUCCESS_MESSAGE);
            map.put("message", "batchDealComplain success");

        } else {
            map.clear();
            map.put(Result.RESULT_CODE, error.getResultCode());
            map.put(Result.RESULT_MESSAGE, error.getResultMessage());
        }
        logger.debug("==> response = {}", JSON.toJSON(map));
        WrenchUtil.responseMap(response, map);
        map = null;
        logger.debug("==>**********exit out batchDealComplain(HttpServletRequest request, HttpServletResponse response, ModelMap map)**********");

    }


}
